#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <functional>

using namespace std;

template <class T>
using func_t = function<void(T&)>;

template <class T>
class Thread
{
public:
    Thread(string threadname, func_t<T> fc, T data)
        :
        _tid(0)
        ,_func(fc)
        ,_threadname(threadname)
        ,_isrunning(false)
        ,_data(data)
    {}

    static void *threadroutine(void *args)
    {
        //(void *) args;
        Thread *ts = static_cast<Thread *>(args);

        ts->_func(ts->_data); 

        return nullptr;
    }

    bool Start()
    {
        int n = pthread_create(&_tid, nullptr, threadroutine, this);
        if(n == 0)
        {
            _isrunning = true;
            return true;
        }
        return false;
    }

    bool Join()
    {
        if(_isrunning == false) return true;

        int n = pthread_join(_tid, nullptr);
        if(n == 0)
        {
            _isrunning = false;
            return true;
        }
        return false;
    }

    string ThreadName()
    {
        return _threadname;
    }

    bool IsRunning()
    {
        return _isrunning;
    }

    ~Thread()
    {}
private:
    pthread_t _tid;
    string _threadname;
    bool _isrunning;
    func_t<T> _func;
    T _data;
};